const express = require('express');
      bodyParser = require('body-parser');
      cookieParser = require('cookie-parser'),
      multer = require('multer');
      User = require('./db/db.js'),
      app = express();
app.use(express.static('www'));
app.use(bodyParser.urlencoded({ extended: true }))
app.get('', (req, res) => {
    console.log('这是根目录');
})
app.post('/api/zhuce',(req,res) => {
    var user = req.body;
    if(user.password == user.mima){
        delete user.mima;
        User.find({name:user.name},function(err,data){
            if(!err){
                if(data.length>0){
                    res.status(200).json({"code":"error","content":"该账号已经被占用"})
                }else{
                    new User(user).save((err)=>{
                        if(!err){
                            res.status(200).json({"code":"success","content":"注册成功"})
                        }
                    })
                }
            }
        })
    }else{
        res.status(200).json({"code":"error","content":"密码不一致"})
    }
})
app.post('/api/denglu',(req,res) => {
    var user = req.body;
    User.find({ name: user.name }, function(err, data) {
        if(!err){
            if(data.length > 0){
                var isIn = (data[0].name == user.name && data[0].password == user.password);
                if(isIn){
                    res.status(200).json({"code":"success","content":"登录成功",data:user})
                }else{
                    res.status(200).json({"code":"error","content":"用户名或密码不正确"})
                }
            }else{res.status(200).json({"code":"error","content":"用户名或密码不正确"})}
        }
    })
})                       
app.listen(3000, () => {
  console.log('服务器正常起动');
})
